/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dao.McKitDAO;
import dao.StReproducaoMaterialDAO;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.McKit;
import model.StReproducaoMaterial;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

/**
 *
 * @author CAR
 */
public class atualizaStReproducaoMaterial extends HttpServlet {


    public atualizaStReproducaoMaterial(){
            super();
    }
    
    private String storeId;
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        //parse JSON object and populate the Customer bean 

        
        updateStReproducaoMaterial(request, response);
        
        //get the store Id from the request
//        storeId = request.getParameter("store_id");
        //get the request type update or insert
//        String action = request.getParameter("action");

        //if update
//        if (action.trim().equalsIgnoreCase("update")) {
//            updateDiaria(request, response);
//        } //if insert
//        else if (action.trim().equalsIgnoreCase("insert")) {
//            insertCustomer(request, response);
//        }
    }

    private void updateStReproducaoMaterial(HttpServletRequest request, HttpServletResponse response) throws IOException {

        //get the record Information 
        String recordInfo = request.getParameter("recordInfo");

        //parse JSON object and populate the Customer bean 
        JSONObject stReproducaoMaterialObj = (JSONObject) JSONSerializer.toJSON(recordInfo);
        StReproducaoMaterial stReproducaoMaterial = (StReproducaoMaterial) JSONObject.toBean(stReproducaoMaterialObj, StReproducaoMaterial.class);
        
        StReproducaoMaterialDAO stReproducaoMaterialDAO = new StReproducaoMaterialDAO();
        
        int qntExpDisciplinas = stReproducaoMaterial.getQntAlunos() + stReproducaoMaterial.getQntTutores() + stReproducaoMaterial.getQntPolos() + stReproducaoMaterial.getQntProf() + stReproducaoMaterial.getQntDisciplinas();
        float qntTotal = qntExpDisciplinas * stReproducaoMaterial.getQntDisciplinas();
        float vlrFinal = qntTotal * stReproducaoMaterial.getVlrUnit();
        
        stReproducaoMaterial.setQntTotal(qntTotal);
        stReproducaoMaterial.setVlrFinal(vlrFinal);
        stReproducaoMaterial.setQntExemplarDisc(qntExpDisciplinas);
        
        boolean success = stReproducaoMaterialDAO.updateStReproducaoMaterial(storeId, stReproducaoMaterial);

        PrintWriter out = response.getWriter();
        response.setContentType("text/html");

        //send response back whether the request was successful
        JSONObject myObj = new JSONObject();
        myObj.put("success", success);
        myObj.put("streproducaomaterial",stReproducaoMaterial);
        out.println(myObj.toString());
        out.close();

    }

}
    